package com.ruoyi.business.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.domain.BusinessMessage;
import com.ruoyi.business.service.IBusinessMessageService;

/**
 * 消息管理 信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/business/message")
public class MessageController extends BaseController
{
    private String prefix = "business/message";

    @Autowired
    private IBusinessMessageService businessMessageService;

    @RequiresPermissions("business:message:view")
    @GetMapping()
    public String message()
    {
        return prefix + "/message";
    }

    /**
     * 查询消息列表
     */
    @RequiresPermissions("business:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessMessage businessMessage)
    {
        startPage();
        List<BusinessMessage> list = businessMessageService.selectBusinessMessageList(businessMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @RequiresPermissions("business:message:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessMessage businessMessage)
    {
        List<BusinessMessage> list = businessMessageService.selectBusinessMessageList(businessMessage);
        ExcelUtil<BusinessMessage> util = new ExcelUtil<BusinessMessage>(BusinessMessage.class);
        return util.exportExcel(list, "消息数据");
    }

    /**
     * 新增消息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息
     */
    @RequiresPermissions("business:message:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessMessage businessMessage)
    {
        return toAjax(businessMessageService.insertBusinessMessage(businessMessage));
    }

    /**
     * 修改消息
     */
    @RequiresPermissions("business:message:edit")
    @GetMapping("/edit/{messageId}")
    public String edit(@PathVariable("messageId") Long messageId, ModelMap mmap)
    {
        BusinessMessage businessMessage = businessMessageService.selectBusinessMessageById(messageId);
        mmap.put("businessMessage", businessMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @RequiresPermissions("business:message:edit")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessMessage businessMessage)
    {
        return toAjax(businessMessageService.updateBusinessMessage(businessMessage));
    }

    /**
     * 删除消息
     */
    @RequiresPermissions("business:message:remove")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(businessMessageService.deleteBusinessMessageByIds(ids));
    }
    
    /**
     * 创建消息
     */
    @RequiresPermissions("business:message:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/createFromReminder/{reminderId}")
    @ResponseBody
    public AjaxResult createFromReminder(@PathVariable("reminderId") Long reminderId)
    {
        return toAjax(businessMessageService.createMessageFromReminder(reminderId));
    }
    
    /**
     * 发送消息
     */
    @RequiresPermissions("business:message:edit")
    @Log(title = "消息管理", businessType = BusinessType.OTHER)
    @PostMapping("/send/{messageId}")
    @ResponseBody
    public AjaxResult send(@PathVariable("messageId") Long messageId)
    {
        return toAjax(businessMessageService.sendMessage(messageId));
    }
    
    /**
     * 批量发送消息
     */
    @RequiresPermissions("business:message:edit")
    @Log(title = "消息管理", businessType = BusinessType.OTHER)
    @PostMapping("/batchSend")
    @ResponseBody
    public AjaxResult batchSend(String ids)
    {
        return toAjax(businessMessageService.batchSendMessages(Convert.toStrArray(ids)));
    }
    
    /**
     * 查看消息详情
     */
    @RequiresPermissions("business:message:detail")
    @GetMapping("/detail/{messageId}")
    public String detail(@PathVariable("messageId") Long messageId, ModelMap mmap)
    {
        BusinessMessage businessMessage = businessMessageService.selectBusinessMessageById(messageId);
        mmap.put("businessMessage", businessMessage);
        return prefix + "/detail";
    }
} 